package br.ucb.cm.main.mainview.ui;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.Color;
import java.awt.Font;
import javax.swing.SwingConstants;
import br.ucb.cm.main.common.GeneralConstants;
import br.ucb.cm.main.common.GeneralConstants.PlayerEnum;
import br.ucb.cm.main.common.GeneralConstants.PlayerTypeEnum;
import br.ucb.cm.main.mainview.ui.IfcNewGame.ActionEnum;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class NewGameUI extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField playerIName;
	private JLabel difficultyLevel;
	private JSlider difficultySlider;
	private IfcNewGame controller;
	private JPanel difficultyPanel;
	private JPanel playerINamePanel;
	private JButton okButton;
	private JButton cancelButton;
	private JComboBox<String> p1ComboBox;
	private JComboBox<String> p2ComboBox;
	private JComboBox<String> startingPlayer;
	private JPanel playerIINamePanel;
	private JTextField playerIIName;

	public NewGameUI(IfcNewGame controller) {
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setModal(true);
		this.controller = controller;
		initComponents();
	}

	private void initComponents() {
		setResizable(false);
		setTitle("Novo Jogo");		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 231, 454);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		playerINamePanel = new JPanel();
		playerINamePanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Nome do Jogador 1", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		difficultyPanel = new JPanel();
		difficultyPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Dificuldade", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel startingPlayerPanel = new JPanel();
		startingPlayerPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Configura\u00E7\u00E3o de Jogador", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		playerIINamePanel = new JPanel();
		playerIINamePanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Nome do Jogador 2", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		playerIINamePanel.setLayout(new BorderLayout(0, 0));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(playerINamePanel, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
				.addComponent(difficultyPanel, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
				.addComponent(startingPlayerPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
				.addComponent(playerIINamePanel, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
				);
		gl_contentPanel.setVerticalGroup(
				gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
						.addComponent(playerINamePanel, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(playerIINamePanel, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
						.addComponent(difficultyPanel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(startingPlayerPanel, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
						.addGap(18))
				);

		playerIIName = new JTextField();
		playerIIName.setHorizontalAlignment(SwingConstants.CENTER);
		playerIIName.setFont(new Font("Papyrus", Font.BOLD, 13));
		playerIIName.setColumns(10);
		playerIINamePanel.add(playerIIName, BorderLayout.CENTER);
		startingPlayerPanel.setLayout(null);

		JLabel lblJogador = new JLabel("Jogador 1:");
		lblJogador.setBounds(10, 27, 64, 14);
		startingPlayerPanel.add(lblJogador);

		JLabel lblJogador_1 = new JLabel("Jogador 2:");
		lblJogador_1.setBounds(10, 56, 64, 14);
		startingPlayerPanel.add(lblJogador_1);

		p1ComboBox = new JComboBox<String>();
		p1ComboBox.setModel(new PlayerTypeCompoBox());
		p1ComboBox.setSelectedIndex(0);
		p1ComboBox.setBounds(68, 24, 119, 20);
		startingPlayerPanel.add(p1ComboBox);

		p2ComboBox = new JComboBox<String>();
		p2ComboBox.setModel(new PlayerTypeCompoBox());
		p2ComboBox.setSelectedIndex(1);
		p2ComboBox.setBounds(68, 52, 119, 20);
		startingPlayerPanel.add(p2ComboBox);

		JLabel lblInicia = new JLabel("Inicia:");
		lblInicia.setBounds(10, 85, 64, 14);
		startingPlayerPanel.add(lblInicia);

		startingPlayer = new JComboBox<String>();
		startingPlayer.setModel(new PlayerCompoBox());
		startingPlayer.setSelectedIndex(0);
		startingPlayer.setBounds(68, 81, 119, 20);
		startingPlayerPanel.add(startingPlayer);

		difficultySlider = new JSlider();
		difficultyLevel = new JLabel("3");
		difficultyLevel.setFont(new Font("Tahoma", Font.BOLD, 14));
		difficultySlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				int difficulty = difficultySlider.getValue();
				difficultyLevel.setText(String.valueOf(difficulty));
				difficultySlider.setBackground(new Color(difficulty * (255 / GeneralConstants.MAX_DIFFICULTY), 255 - difficulty * (255 / GeneralConstants.MAX_DIFFICULTY), 0));
			}
		});
		difficultySlider.setValue(3);
		difficultySlider.setMaximum(GeneralConstants.MAX_DIFFICULTY);
		difficultySlider.setMinimum(1);
		difficultyPanel.add(difficultySlider);
		difficultyPanel.add(difficultyLevel);
		playerINamePanel.setLayout(new BorderLayout(0, 0));

		playerIName = new JTextField();
		playerIName.setHorizontalAlignment(SwingConstants.CENTER);
		playerIName.setFont(new Font("Papyrus", Font.BOLD, 13));
		playerINamePanel.add(playerIName);
		playerIName.setColumns(10);
		contentPanel.setLayout(gl_contentPanel);
		{
			okButton = new JButton("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getController().doAction(
							ActionEnum.SET_CONFIG, 
							playerIName.getText(), 
							playerIIName.getText(),
							difficultySlider.getValue(), 

							PlayerTypeEnum.getByOrdinal(p1ComboBox.getSelectedIndex()),
							PlayerTypeEnum.getByOrdinal(p2ComboBox.getSelectedIndex()),
							PlayerEnum.getByOrdinal(startingPlayer.getSelectedIndex()));
					dispose();
				}
			});
			okButton.setMnemonic('O');
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			cancelButton = new JButton("Cancelar");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getController().doAction(ActionEnum.CANCEL);
					dispose();
				}
			});
			cancelButton.setMnemonic('C');
			cancelButton.setActionCommand("Cancel");
		}
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(contentPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap(87, Short.MAX_VALUE)
						.addComponent(okButton)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(cancelButton)
						.addContainerGap())
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 245, Short.MAX_VALUE)
						.addGap(18)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(cancelButton)
								.addComponent(okButton))
								.addContainerGap())
				);
		getContentPane().setLayout(groupLayout);
		setLocationRelativeTo(null);
	}

	class PlayerTypeCompoBox extends DefaultComboBoxModel<String>{
		private static final long serialVersionUID = 1L;

		public PlayerTypeCompoBox() {
			super(new String[] {"Jogador", "Computador"});
		}
	}

	class PlayerCompoBox extends DefaultComboBoxModel<String>{
		private static final long serialVersionUID = 1L;

		public PlayerCompoBox() {
			super(new String[] {"Jogador 1", "Jogador 2"});
		}
	}

	public IfcNewGame getController() {
		return controller;
	}

	public void setController(IfcNewGame controller) {
		this.controller = controller;
	}
}
